<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="用户编号" prop="userNum">
        <el-input
          v-model="queryParams.userNum"
          placeholder="请输入用户编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="用户名" prop="userName">
        <el-input
          v-model="queryParams.userName"
          placeholder="请输入用户名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input
          v-model="queryParams.password"
          placeholder="请输入密码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否认证： 0是  1否" prop="isApprove">
        <el-input
          v-model="queryParams.isApprove"
          placeholder="请输入是否认证： 0是  1否"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否房东：0是 1否" prop="isLandlord">
        <el-input
          v-model="queryParams.isLandlord"
          placeholder="请输入是否房东：0是 1否"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="用户创建时间" prop="creatTime">
        <el-date-picker
          clearable
          v-model="queryParams.creatTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择用户创建时间"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['user:user:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['user:user:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['user:user:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['user:user:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="userList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />

      <el-table-column label="用户编号" align="center" prop="userNum" />
      <el-table-column label="姓名" align="center" prop="realName" />
      <el-table-column label="性别" align="center" prop="sex" />
      <el-table-column label="手机号" align="center" prop="phone" />
      <el-table-column label="身份证号" align="center" prop="idCard" />
      <el-table-column label="职业信息" align="center" prop="occupation" />
      <el-table-column
        label="学历"
        align="center"
        prop="schoolExperienceType"
      />
      <el-table-column label="是否认证" align="center" prop="isApprove">
        <template slot-scope="scope">
          {{ scope.row.isApprove == 0 ? "是" : "否" }}
        </template>
      </el-table-column>
      <el-table-column label="是否房东" align="center" prop="isLandlord">
        <template slot-scope="scope">
          {{ scope.row.isLandlord == 0 ? "是" : "否" }}
        </template></el-table-column
      >

      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['renter:user:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['renter:user:remove']"
            >删除</el-button
          >

          <el-button
            v-if="scope.row.isApprove == 1"
            size="mini"
            type="text"
            icon="el-icon-search"
            @click="(dialogFormVisible = true), zoot(scope.row)"
            >实名认证</el-button
          >

          <el-button
            size="mini"
            type="text"
            icon="el-icon-search"
            @click="lookText(scope.row.id), (rooot = true)"
            v-hasPermi="['user:user:lookText']"
            >查看详情</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <div>
      <el-dialog title="用户信息认证" :visible.sync="dialogFormVisible">
        <el-form ref="forms" :model="forms" :rules="rules" label-width="80px">
          <el-form-item label="姓名" prop="realName">
            <el-input
              v-model="forms.approve.realName"
              placeholder="请输入用户名"
            />
          </el-form-item>
          <el-form-item label="租客电话" prop="phone">
            <el-input
              v-model="forms.approve.phone"
              placeholder="请输入手机号"
            />
          </el-form-item>
          <el-form-item label="职业" prop="occupation">
            <el-input
              v-model="forms.approve.occupation"
              placeholder="请输入职业类型"
            />
          </el-form-item>
          <el-form-item label="性别" prop="sex">
            <el-input v-model="forms.approve.sex" placeholder="请输入性别" />
          </el-form-item>
          <el-form-item label="身份证号" prop="idCard">
            <el-input
              v-model="forms.approve.idCard"
              placeholder="请输入身份证号"
            />
          </el-form-item>

          <el-form-item label="年龄" prop="age">
            <el-input v-model="forms.approve.age" placeholder="请输入年龄" />
          </el-form-item>

          <el-form-item label="学历" prop="schoolId">
            <el-select
              v-model="forms.approve.schoolId"
              placeholder="请选择学历"
            >
              <el-option label="无" value="1"></el-option>
              <el-option label="小学" value="2"></el-option>
              <el-option label="初中" value="3"></el-option>
              <el-option label="高中" value="4"></el-option>
              <el-option label="本科" value="5"></el-option>
            </el-select>
          </el-form-item>

          <el-form-item label="银行卡号" prop="bankCard">
            <el-input
              v-model="forms.approve.bankCard"
              placeholder="请输入银行卡号"
            />
          </el-form-item>

          <!-- <el-upload
            action="http://localhost/dev-api/user/user/upload/"
            list-type="picture-card"
            :on-preview="handlePictureCardPreview"
            :on-success="handleAvatarSuccess"
            :on-remove="handleRemove"
          >
            <i class="el-icon-plus"></i>
          </el-upload>
          <el-dialog :visible.sync="forms.photoInfo.idCardPhoto">
            <img width="100%" :src="forms.photoInfo.idCardPhoto" alt="" />
          </el-dialog> -->

          <el-upload
            class="avatar-uploader"
            action="http://localhost/dev-api/user/user/upload/"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
          >
            <img
              v-if="forms.photoInfo.idCardPhoto"
              :src="forms.photoInfo.idCardPhoto"
              class="avatar"
            />
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取 消</el-button>
          <!-- v-hasPermi="['user:user:approveMy']" -->
          <el-button
            type="primary"
            @click="approveMy(), (dialogFormVisible = false)"
            >确 定</el-button
          >
        </div>
      </el-dialog>
    </div>

    <div>
      <el-dialog title="用户信息详情" :visible.sync="rooot">
        <el-form ref="boxbox" :model="boxbox" :rules="rules" label-width="80px">
          姓名:{{ boxbox.realName }} &emsp13; &emsp13;&emsp13;&emsp13;&emsp13;
          &emsp13;&emsp13;&emsp13; &emsp13;&emsp13; 性别:{{ boxbox.sex
          }}<br /><br />

          租客电话:{{ boxbox.phone }} &emsp13; &emsp13;&emsp13;&emsp13;
          &emsp13;&emsp13; 身份证号:{{ boxbox.idCard }}<br /><br />
          性别:{{ boxbox.sex }} &emsp13; &emsp13;&emsp13;&emsp13;
          &emsp13;&emsp13; 职业:{{ boxbox.occupation
          }}<br /><br /><br /><br /><br /><br />

          认证信息<br /><br /><br />
          学历:{{
            boxbox.schoolId == 1
              ? "无"
              : boxbox.schoolId == 2
              ? "小学"
              : boxbox.schoolId == 3
              ? "初中"
              : boxbox.schoolId == 4
              ? "高中"
              : boxbox.schoolId == 5
              ? "本科"
              : "未认证"
          }}<br /><br />
          职业信息:{{ boxbox.occupation }}<br /><br />
          银行卡号:{{ boxbox.idCard }}<br /><br />
        </el-form>

        <div slot="footer" class="dialog-footer">
          <el-button @click="rooot = false">取 消</el-button>
          <el-button type="primary" @click="rooot = false">确 定</el-button>
        </div>
      </el-dialog>
    </div>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改账单管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="用户编号" prop="userNum">
          <el-input v-model="form.userNum" placeholder="请输入用户编号" />
        </el-form-item>
        <el-form-item label="用户名" prop="userName">
          <el-input v-model="form.userName" placeholder="请输入用户名" />
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="form.password" placeholder="请输入密码" />
        </el-form-item>
        <el-form-item label="是否认证： 0是  1否" prop="isApprove">
          <el-input
            v-model="form.isApprove"
            placeholder="请输入是否认证： 0是  1否"
          />
        </el-form-item>
        <el-form-item label="是否房东：0是 1否" prop="isLandlord">
          <el-input
            v-model="form.isLandlord"
            placeholder="请输入是否房东：0是 1否"
          />
        </el-form-item>
        <el-form-item label="用户创建时间" prop="creatTime">
          <el-date-picker
            clearable
            v-model="form.creatTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择用户创建时间"
          >
          </el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  lookText,
  approveList,
  listUser,
  getUser,
  delUser,
  addUser,
  updateUser,
} from "@/api/user/user";

export default {
  name: "User",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,

      imageUrl: "",
      // 账单管理表格数据
      userList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userNum: null,
        userName: null,
        password: null,
        isApprove: null,
        isLandlord: null,
        creatTime: null,
      },
      // 表单参数
      form: {},

      dialogFormVisible: false,

      forms: {
        // userId: "",
        // realName: "",
        // phone: "",
        // occupation: "",
        // sex: "",
        // idCard: "",
        // age: "",
        // bankCard: "",
        // schoolId: "",
        approve: {},
        photoInfo: {},
      },
      userInfoId: "",

      rooot: false,
      boxbox: {
        schoolId: "",
        realName: "",
        phone: "",
        occupation: "",
        age: "",
        idCard: "",
        sex: "",
        bankCard: "",
      },

      // 表单校验
      rules: {},
    };
  },
  created() {
    this.getList();
  },
  methods: {
    handleRemove(file, fileList) {
      console.log(file, fileList);
    },

    handleAvatarSuccess(res, file) {
      this.forms.photoInfo.idCardPhoto = res;
    },
    handlePictureCardPreview(resp) {
      this.dialogVisible = true;
    },

    //查看用户详情
    lookText(id) {
      this.rooot = true;
      lookText(id).then((resp) => {
        console.log(resp.data);
        this.boxbox = resp.data;
        this.$message.success("查询成功！");
      });
    },
    zoot(row) {
      this.forms.approve.userId = row.id;
      this.forms.photoInfo.approveId = row.id;

      alert(this.forms.approve.userId);
      alert(this.forms.photoInfo.approveId);
    },

    approveMy() {
      approveList(this.forms).then((resp) => {
        this.$message.success("认证成功！");
        this.getList();
      });
    },

    /** 查询账单管理列表 */
    getList() {
      this.loading = true;
      listUser(this.queryParams).then((response) => {
        this.userList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        userNum: null,
        userName: null,
        password: null,
        isApprove: null,
        isLandlord: null,
        creatTime: null,
        updateTime: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加账单管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;

      getUser(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改账单管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateUser(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addUser(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除账单管理编号为"' + ids + '"的数据项？')
        .then(function () {
          return delUser(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "user/user/export",
        {
          ...this.queryParams,
        },
        `user_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>

<style>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
